package com.ruoyi.web.controller.recipe;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.recipe.domain.RecipeArticle;
import com.ruoyi.recipe.service.IRecipeArticleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章Controller
 * 
 * @author ruoyi
 * @date 2025-02-14
 */
@Controller
@RequestMapping("/recipe/article")
public class RecipeArticleController extends BaseController
{
    private String prefix = "recipe/article";

    @Autowired
    private IRecipeArticleService recipeArticleService;

    @RequiresPermissions("recipe:article:view")
    @GetMapping()
    public String article()
    {
        return prefix + "/article";
    }

    /**
     * 查询文章列表
     */
    @RequiresPermissions("recipe:article:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecipeArticle recipeArticle)
    {
        startPage();
        List<RecipeArticle> list = recipeArticleService.selectRecipeArticleList(recipeArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @RequiresPermissions("recipe:article:export")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecipeArticle recipeArticle)
    {
        List<RecipeArticle> list = recipeArticleService.selectRecipeArticleList(recipeArticle);
        ExcelUtil<RecipeArticle> util = new ExcelUtil<RecipeArticle>(RecipeArticle.class);
        return util.exportExcel(list, "文章数据");
    }

    /**
     * 新增文章
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文章
     */
    @RequiresPermissions("recipe:article:add")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecipeArticle recipeArticle)
    {
        return toAjax(recipeArticleService.insertRecipeArticle(recipeArticle));
    }

    /**
     * 修改文章
     */
    @RequiresPermissions("recipe:article:edit")
    @GetMapping("/edit/{articleId}")
    public String edit(@PathVariable("articleId") Long articleId, ModelMap mmap)
    {
        RecipeArticle recipeArticle = recipeArticleService.selectRecipeArticleByArticleId(articleId);
        mmap.put("recipeArticle", recipeArticle);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章
     */
    @RequiresPermissions("recipe:article:edit")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecipeArticle recipeArticle)
    {
        return toAjax(recipeArticleService.updateRecipeArticle(recipeArticle));
    }

    /**
     * 删除文章
     */
    @RequiresPermissions("recipe:article:remove")
    @Log(title = "文章", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recipeArticleService.deleteRecipeArticleByArticleIds(ids));
    }

    /**
     * 查询公告详细
     */
    @RequiresPermissions("recipe:article:list")
    @GetMapping("/view/{articleId}")
    public String view(@PathVariable("articleId") Long articleId, ModelMap mmap)
    {
        mmap.put("recipeArticle", recipeArticleService.selectRecipeArticleByArticleId(articleId));
        return prefix + "/view";
    }
}
